package com.pureut.order.controller;

import com.pureut.common.core.constant.Constants;
import com.pureut.common.core.domain.R;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.order.export.OrderSupplierManagementimportExport;
import com.pureut.order.util.ExcelUtil;
import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.order.domain.OrderSupplierManagement;
import com.pureut.order.dto.OrderSupplierManagementDto;
import com.pureut.order.export.OrderSupplierManagementExport;
import com.pureut.order.service.OrderSupplierManagementService;
import com.pureut.order.vo.OrderReviewVo;
import com.pureut.order.vo.OrderSupplierManagementVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.List;

/**
 * 供应商管理模块
 */
@RestController
@RequestMapping("/supplier-management")
public class OrderSupplierManagementController extends BaseController {


    @Resource
    OrderSupplierManagementService orderSupplierManagementService;

    /**
     * 供应商管理列表
     */
    @RequiresPermissions("order:supplier:list")
    @GetMapping("/list")
    public TableDataInfo list(OrderSupplierManagementVo orderSupplierManagementVo) {
        startPage();
        //查询供应商列表
        List<OrderSupplierManagementDto> list = orderSupplierManagementService.getList(orderSupplierManagementVo);
        return getDataTable(list);
    }

    /**
     * 供应商新增
     */
    @RequiresPermissions("order:purchase:supplier:add")
    @Log(title = "供应商新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody OrderSupplierManagementVo orderSupplierManagementVo) throws Exception {
        return toAjax(orderSupplierManagementService.addSupplier(orderSupplierManagementVo));
    }

    /**
     * 供应商修改
     */
    @Log(title = "供应商修改", businessType = BusinessType.UPDATE)
    @RequiresPermissions("order:purchase:supplier:edit")
    @PostMapping("/update")
    public AjaxResult update(@Validated @RequestBody OrderSupplierManagementVo orderSupplierManagementVo) throws Exception {
        return toAjax(orderSupplierManagementService.updateSupplierById(orderSupplierManagementVo));
    }

    /**
     * 供应商删除
     */
    @RequiresPermissions("order:purchase:supplier:remove")
    @Log(title = "供应商删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable("ids") String ids) throws Exception {
        return toAjax(orderSupplierManagementService.deleteSupplierByIds(ids));
    }

    /**
     * 查看供应商信息
     */
    @GetMapping("/view/{id}")
    @RequiresPermissions("order:purchase:supplier:query")
    public AjaxResult view(@PathVariable("id") Long id){
        return AjaxResult.success(orderSupplierManagementService.getSupplierById(id));
    }

    /**
     * 冻结供应商
     */
    @RequiresPermissions("order:purchase:supplier:frozen")
    @Log(title = "冻结供应商", businessType = BusinessType.UPDATE)
    @GetMapping("/frozen/{id}")
    public AjaxResult frozenSupplier(@PathVariable("id") Long id){
        return toAjax(orderSupplierManagementService.frozenSupplier(id));
    }

    /**
     * 解冻供应商
     */
    @RequiresPermissions("order:purchase:supplier:thaw")
    @Log(title = "解冻供应商", businessType = BusinessType.UPDATE)
    @GetMapping("/thaw/{id}")
    public AjaxResult thawSupplier(@PathVariable("id") Long id){
        return toAjax(orderSupplierManagementService.thawSupplier(id));
    }

    /**
     * 撤销审核
     */
    @RequiresPermissions("order:purchase:supplier:revoke")
    @Log(title = "撤销审核", businessType = BusinessType.UPDATE)
    @GetMapping("/revoke/{id}")
    public AjaxResult revokeApproval(@PathVariable("id") Long id){
        return toAjax(orderSupplierManagementService.revokeApproval(id));
    }

//    /**
//     * 生成供应商导入模板
//     */
//    @PostMapping("/supplierTemplate")
//    public AjaxResult supplierTemplate(HttpServletResponse response) throws IOException {
//        ExcelUtil<OrderSupplierManagementimportExport> excelUtil = new ExcelUtil<OrderSupplierManagementimportExport>(OrderSupplierManagementimportExport.class);
//        excelUtil.exportTemplate(response, null, "供应商信息.xlsx");
//        return AjaxResult.success();
//    }

    /**
     * 生成供应商导入模板
     */
    @PostMapping("/downTemplate")
    public void downTemplate(HttpServletResponse response) throws IOException {
        String name = "供应商";
        String fileName = Constants.STATIC_URL + "导入" + name + ".xlsx";
        File file = new File(fileName);
        ByteArrayOutputStream baos = null;
        ServletOutputStream out = null;
        try {

            InputStream inStream=new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int len;
            baos = new ByteArrayOutputStream();
            while ((len = inStream.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode("导入供应商.xlsx","UTF-8"));
            out = response.getOutputStream();
            out.write(baos.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            baos.flush();
            baos.close();
            out.flush();
            out.close();
        }
    }

    /**
     * 供应商信息导出
     */
    @RequiresPermissions("order:purchase:supplier:export")
    @PostMapping("/supplierMessageExport")
    public AjaxResult mouldMessageExport(HttpServletResponse response,OrderSupplierManagementVo orderSupplierManagementVo) throws IOException {
        List<OrderSupplierManagementExport> infoExport = orderSupplierManagementService.getInfoExport(orderSupplierManagementVo);
        ExcelUtil<OrderSupplierManagementExport> excelUtil = new ExcelUtil<OrderSupplierManagementExport>(OrderSupplierManagementExport.class);
        excelUtil.excelWriter(response, infoExport, "供应商信息信息.xlsx");
        return AjaxResult.success();
    }

    /**
     * 供应商信息导入
     */
    @RequiresPermissions("order:purchase:supplier:import")
    @Log(title = "供应商信息导入", businessType = BusinessType.INSERT)
    @PostMapping("/supplierMessageImport")
    public AjaxResult mouldMessageImport(MultipartFile file) throws IOException {
        ExcelUtil<OrderSupplierManagementimportExport> excelUtil = new ExcelUtil<OrderSupplierManagementimportExport>(OrderSupplierManagementimportExport.class);
        List<OrderSupplierManagementimportExport> coCustomer = excelUtil.importExcel(file.getInputStream());
        return AjaxResult.success(orderSupplierManagementService.importMouldData(coCustomer));
    }

    /**
     * 提交审核
     */

    @RequiresPermissions("order:purchase:supplier:subExamine")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE)
    @GetMapping("/reviewSubmit/{id}")
    public AjaxResult reivewSubmit(@PathVariable("id") Long id){
        return toAjax(orderSupplierManagementService.reivewSubmit(id));
    }

    /**
     * 供应商审核
     */
    @RequiresPermissions("order:purchase:supplier:examine")
    @Log(title = "供应商审核", businessType = BusinessType.UPDATE)
    @PostMapping("/supplierReview")
    public AjaxResult supplierReview(@Validated @RequestBody OrderReviewVo orderReviewVo) {
        return toAjax(orderSupplierManagementService.supplierReview(orderReviewVo));
    }
}
